package cn.kgc.vue.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Score;
import cn.kgc.vue.service.ScoreService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 课工场
 * @date 2023/12/15
 * @description
 */

@Api(tags = "成绩模块")
@RestController
@RequestMapping("scores")
public class ScoreController {

    @Resource
    private ScoreService scoreService;

    @ApiOperation("成绩列表")
    @PostMapping("list")
    public ResponseResult getScoreList(@RequestBody  QueryCondition<Score> queryCondition){
        return scoreService.getScoreList(queryCondition);
    }

    @ApiOperation("添加/修改成绩信息")
    @PostMapping("saveOrUpdate")
    public ResponseResult saveOrUpdateScore(@RequestBody  Score score){


        //1. 判定 在同一个时间该学生是否已经有考试成绩
        Integer id = score.getId();

        if (ObjectUtils.isNull(id)){
            LambdaQueryWrapper<Score> lambda = new QueryWrapper<Score>().lambda();
            lambda.eq(Score::getStudentNo,score.getStudentNo())
                    .eq(Score::getExamTime,score.getExamTime());
            Score one = scoreService.getOne(lambda);
            if (ObjectUtils.isNotNull(one)){
                return  ResponseResult.fail().message("同一个考试时间,不能添加两次成绩");
            }
        }

        scoreService.saveOrUpdate(score);

        return ResponseResult.success();
    }


    @ApiOperation("根据id获取成绩信息")
    @GetMapping ("getById/{id}")
    public ResponseResult getScoreById(@PathVariable Integer id){
        Score score = scoreService.getById(id);
        return ResponseResult.success().data(score);
    }



    //batchImport
    @ApiOperation("批量导入学生成绩")
    @PostMapping("batchImport")
    public ResponseResult batchImportScore(MultipartFile file){

        return scoreService.batchImportScore(file);
    }


    @ApiOperation("根据id删除学生成绩")
    @DeleteMapping("delete/{id}")
    public ResponseResult removeScore(@PathVariable Integer id){
        scoreService.removeById(id);
        return ResponseResult.success().message("删除成功");
    }


    @ApiOperation("导出学生成绩")
    @GetMapping ("excel")
    public void exportScore(HttpServletResponse response, String clazz, String examTime) throws Exception {

        // 改变日期格式
        Date date = null;
        if (StringUtils.isNotEmpty(examTime)){
            date  = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse(examTime);
        }

        LambdaQueryWrapper<Score> lambda = new QueryWrapper<Score>().lambda();
        lambda.eq(StringUtils.isNotEmpty(clazz),Score::getClazz,clazz)
                .eq(StringUtils.isNotEmpty(examTime),Score::getExamTime,date);

        List<Score> list = scoreService.list(lambda);

        ExportParams exportParams = new ExportParams("学生成绩信息表", "学生成绩");

        Workbook Workbook = ExcelExportUtil.exportExcel(exportParams, Score.class, list);
        ServletOutputStream outputStream = response.getOutputStream();
        Workbook.write(outputStream);
        Workbook.close();
        outputStream.close();
    }


    // screenData
    @ApiOperation("数据可视化接口")
    @GetMapping ("screenData")
    public ResponseResult screenData() {
        return  scoreService.screenData();
    }


}
